package com.vincent.controller.admin;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.enums.SessionEnum;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.service.RoleService;
import com.vincent.vo.RoleAddVo;
import com.vincent.vo.RoleEditVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @RequestMapping("/manage")
    public String manage(){
        return "/admin/role/manage";
    }

    @RequestMapping(value = "/getRolesManageAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody Object getRolesManageAjax(){
        Map<String,Object> map=new HashMap<String, Object>();
        Map<String, Object> serviceMap = roleService.getRoleManage();
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            map.put(ResultMapKey.ERROR.getCode(),errorObj);
            return map;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        map.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return map;
    }
    @RequestMapping(value = "/add")
    public String add(){
        return "/admin/role/add";
    }

    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable Integer id,HttpSession session,HttpServletRequest request){
        Map<String, Object> serviceMap = roleService.getRole(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/role/manage.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        request.setAttribute("data",successObj);
        return "/admin/role/edit";
    }
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute RoleAddVo vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = roleService.saveRole(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/role/add.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/role/manage.html";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute RoleEditVo vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = roleService.updateRole(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/role/edit/"+vo.getId()+".html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/role/manage.html";
    }
    @RequestMapping(value = "/deleteAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody Object deleteAjax(@RequestParam Integer id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = roleService.deleteRole(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }

}
